package com.example.wechatlistview.tools;

import androidx.annotation.LayoutRes;
import androidx.annotation.Nullable;

import com.example.wechatlistview.R;
import com.example.wechatlistview.controls.ListSection;
import com.example.wechatlistview.controls.SimperListAdapter;
import com.example.wechatlistview.resmanager.itemType;

import java.util.List;
import java.util.Map;

import io.github.luizgrp.sectionedrecyclerviewadapter.SectionedRecyclerViewAdapter;

/**
 * @author CVLocke 陈炜龙
 * RecyclerView列表分组类
 */
public class SectionCreater {

    /**
     * 预定义头与内容布局资源
     */
    public static int itemLayout_discover = R.layout.listview_discover;
    public static int headLayout_discover = R.layout.headview_discover;

    /**
     * 分组适配器创建器
     * 静态方法
     *
     * @param type     ViewHolder种类
     * @param key      键数组
     * @param resArray 资源数组集
     * @param cutIndex 拆分索引数组
     * @param listener 点击事件监听器
     * @return 自动生成分组适配器
     */
    public static SectionedRecyclerViewAdapter create(itemType.holderType type, String[] key, Object[][] resArray, int[] cutIndex, @Nullable ListSection.OnRecyclerViewItemClickListener listener) {

        //定义适配器与布局
        SectionedRecyclerViewAdapter sectionedAdapter = new SectionedRecyclerViewAdapter();
        @LayoutRes int itemLayout = itemLayout_discover;
        @LayoutRes int headLayout = headLayout_discover;

        //存放拆分资源数组
        Object[][][] sectionedResArrays = new Object[cutIndex.length][resArray.length][];

        //遍历索引数组截取资源
        for (int i = 0; i < resArray.length; i++) {
            for (int j = 0; j < cutIndex.length; j++) {
                int prior = j - 1;
                Object[] result;
                if (j == 0) {
                    result = ArrayTools.cutArrayFromHead(resArray[i], cutIndex[j]);
                } else {
                    result = ArrayTools.cutArrayFromPosition(resArray[i], cutIndex[prior], cutIndex[j]);
                }
                sectionedResArrays[j][i] = result;
            }

        }


        //判断种类改变布局
        switch (type) {
            case discover:
                itemLayout = itemLayout_discover;
                headLayout = headLayout_discover;

                break;
            default:
                break;

        }

        //遍历索引数组将列表分组
        for (int i = 0; i < cutIndex.length; i++) {
            List<Map<String, Object>> list = SimperListAdapter.GetProcessedList(key, sectionedResArrays[i]);
            ListSection listSection = new ListSection(list, type, key, itemLayout, headLayout);
            if (listener != null) {
                listSection.setOnRecyclerViewItemClickListener(listener);
            }
            sectionedAdapter.addSection(listSection);
        }

        return sectionedAdapter;

    }

    public static SectionedRecyclerViewAdapter create(String[] key, Object[][] resArray, int[] cutIndex, @LayoutRes int itemId, @LayoutRes int headId) {
        //定义适配器
        SectionedRecyclerViewAdapter sectionedAdapter = new SectionedRecyclerViewAdapter();
        //存放拆分资源数组
        Object[][][] sectionedResArrays = new Object[cutIndex.length][resArray.length][];

        //遍历索引数组截取资源
        for (int i = 0; i < resArray.length; i++) {
            for (int j = 0; j < cutIndex.length; j++) {
                int prior = j - 1;
                Object[] result;
                if (j == 0) {
                    result = ArrayTools.cutArrayFromHead(resArray[i], cutIndex[j]);
                } else {
                    result = ArrayTools.cutArrayFromPosition(resArray[i], cutIndex[prior], cutIndex[j]);
                }
                sectionedResArrays[j][i] = result;
            }
        }

        for (int i = 0; i < cutIndex.length; i++) {
            List<Map<String, Object>> list = SimperListAdapter.GetProcessedList(key, sectionedResArrays[i]);
            ListSection listSection = new ListSection(list, itemType.holderType.none, key, itemId, headId);
            sectionedAdapter.addSection(listSection);
        }

        return sectionedAdapter;
    }


}
